/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.persistance;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import nominalitzacions.domain.Language;

/**
 *
 * @author genis.sole
 */
public class LanguageDataManager {
     private TreeMap<String, File> languages;
     private String mainFolder;

     public LanguageDataManager(String mainFolderName) {
         languages = new TreeMap<String, File>();
         this.mainFolder = mainFolderName;
         iniLangs(mainFolder);
     }

     public void saveLanguage(String langName, String irrPath, String infPath,
             String nounsPath, String corpPath) throws IOException {
         File fc = languages.remove(langName);
         if (fc != null) {
             fc.delete();
         }
         String separator = System.getProperty("file.separator");
         File langFile = new File(System.getProperty("user.home") + separator
                 + mainFolder + separator + "languages" + separator
                 + langName + ".lang");

         langFile.createNewFile();
         
         FileWriter flw = new FileWriter(langFile);
         BufferedWriter bw = new BufferedWriter(flw);
         
         bw.write(infPath);
         bw.newLine();
         bw.write(irrPath);
         bw.newLine();
         bw.write(nounsPath);
         bw.newLine();
         bw.write(corpPath);
         bw.newLine();
         
         bw.close();

         languages.put(langName, langFile);
     }

     public Language loadLanguage(String langName) throws FileNotFoundException {
         Scanner sc = new Scanner(languages.get(langName));
         Language lang = new Language();

         lang.setLanguageName(langName);
         if (sc.hasNextLine()) {
            lang.setDicoInfPath(sc.nextLine());
         }
         if (sc.hasNextLine()) {
            lang.setDicoIrregPath(sc.nextLine());
         }
         if (sc.hasNextLine()) {
            lang.setDicoNamePath(sc.nextLine());
         }
         if (sc.hasNextLine()) {
            lang.setCorpusPath(sc.nextLine());
         }
         sc.close();

         return lang;
     }

     public Language [] loadAllLanguages() throws FileNotFoundException {
        int length = languages.size();
        Language[] ls = new Language[length];
        Set<Map.Entry<String, File> > langs = languages.entrySet();
        Iterator<Map.Entry<String, File> > lit = langs.iterator();
        for (int i = 0; i < length; ++i) {
            Map.Entry<String, File> lang = lit.next();
            ls[i] = loadLanguage(lang.getKey());
        }
        return ls;
     }

     public void deleteLanguage(String langName) {
         File f = languages.remove(langName);
         f.delete();
     }

     private void iniLangs(String mainFolder) {
         String separator = System.getProperty("file.separator");
         File dir = new File(System.getProperty("user.home") + separator
                 + mainFolder + separator + "languages");
         if (!dir.exists()) {
            dir.mkdirs();
         }
         File [] files = dir.listFiles();
         for (int i = 0; i < files.length; ++i) {
             if (files[i].getName().endsWith(".lang")) {
                 StringBuilder langName = new StringBuilder(files[i].getName());
                 langName.delete(langName.lastIndexOf("."), langName.length());
                 languages.put(langName.toString(), files[i]);
             }
         }
     }
}
